package com.zj.helloview;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;

import com.zj.helloview.model.User;
import com.zj.helloview.model.TodoMockModel;
import com.zj.helloview.treeviewer.provider.TodoContentProvider;
import com.zj.helloview.treeviewer.provider.TodoLabelProvider;

public class ViewPart1 extends ViewPart {
	public static final String ID = "com.zj.helloview.view";

	private ComboViewer cviewer;
	private TreeViewer tviewer;
	
	boolean pressed = false;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		GridLayout layout = new GridLayout(2, false);
		parent.setLayout(layout);
		Label label = new Label(parent, SWT.NONE);
		label.setText("Please select a value:   ");
		Text text = new Text(parent, SWT.BORDER);
		createDeco(text, "Use CNTL + SPACE to see possible values");
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(data);
		ControlDecoration deco = new ControlDecoration(text, SWT.LEFT);
		deco.setDescriptionText("Use CNTL + SPACE to see possible values");
		deco.setImage(FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION).getImage());
		deco.setShowOnlyOnFocus(true);
		// Help the user with the possible inputs
		// "." and "#" will also activate the content proposals
		char[] autoActivationCharacters = new char[] { '.', '#', ' ' };
		KeyStroke keyStroke;
		try {
			//
			keyStroke = KeyStroke.getInstance("Ctrl+Space");
			// assume that myTextControl has already been created in some way
			ContentProposalAdapter adapter = new ContentProposalAdapter(text, new TextContentAdapter(), new SimpleContentProposalProvider(new String[] {
					"ProposalOne", "ProposalTwo", "ProposalThree" }), keyStroke, autoActivationCharacters);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Label label1 = new Label(parent, SWT.NONE);
		label1.setText("Select a person:");
		cviewer = new ComboViewer(parent, SWT.READ_ONLY | SWT.Resize);
		cviewer.setContentProvider(new ArrayContentProvider());
		cviewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof User) {
					User person = (User) element;
					return person.getFirstName();
				}
				return super.getText(element);
			}
		});
		User[] persons = new User[] { new User("Lars", "Vogel"), new User("Tim", "Taler"), new User("Jim", "Knopf") };
		// Set set the input to the viewer this input will be send to the
		// content provider
		cviewer.setInput(persons);
		// React to the selection of the viewer
		// Note that the viewer return the real object and not just a string
		// representation
		cviewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				System.out.println(((User) selection.getFirstElement()).getLastName());
			}
		});

		// You can select a object directly via the domain object
		User person = persons[0];
		cviewer.setSelection(new StructuredSelection(person));

		Button setStatusLine = new Button(parent, SWT.PUSH);
		setStatusLine.setText("Set Statusline ");

		setStatusLine.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {

				String message = "I would like to say hello to you.";
				if (pressed) {
					message = "Thank you for using me";
				}
				// Get the status line and set the text
				IActionBars bars = getViewSite().getActionBars();
				bars.getStatusLineManager().setMessage(message);
				pressed = !pressed;
			}
		});

//		tviewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
//		tviewer.setContentProvider(new TodoContentProvider());
//		tviewer.setLabelProvider(new TodoLabelProvider());
//		// Expand the tree
//		tviewer.setAutoExpandLevel(2);
//		// Provide the input to the ContentProvider
//		tviewer.setInput(new TodoMockModel());
	}

	private void createDeco(Text text, String s) {

	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		cviewer.getControl().setFocus();
	}
}